function lstinf$(a$,b$,c$)
  adra=1:adrb=1:blngt=len(b$):d$=""
  while adrb<=blngt
    if mid$ (a$,adra,1)=mid$ (b$,adrb,1) then:adrb+=1:end if
    adra=adra+1
    end while
  while mid$(a$,adra,1)<>left$(c$,1)
    d$=d$+mid$(a$,adra,1):adra=adra+1
    end while
  lstinf$=d$
  end function

function vldc(a$)
  t0$="!"+a$+".0.0.0."
  t1$=lstinf$(t0$,"!",".")
  t2$=lstinf$(t0$,".",".")
  t3=10^(len(t2$))
  vldc=val(t1$)+(val(t2$)/t3)
  end function

dim chfq[16],chvl[16]

dim wlt[16]
wlt[0]=0:wlt[1]=1:wlt[2]=3:wlt[3]=4
wlt[4]=5:wlt[5]=7:wlt[6]=8:wlt[7]=9 '
wlt[8]=11:wlt[9]=12:wlt[10]=13:wlt[11]=15 '
wlt[12]=15:wlt[13]=15:wlt[14]=15:wlt[15]=15
' 0 2 3 4 6 7 8 10 11 12 14 15

finp$="_.csv"
if argc>2 then:finp$=argv(2):end if

print"56676D20" '- vgm
print"FFFFFF0F" '- lenght
print"60010000" '- version
print"0000000000000000" '- clock: sn76489,ym2413
print"D50B0000" '- gd3
print"50D80400" '- total samples
print"67000000" '- loop
print"00000000" '- loop
print"3C000000" '- rate
print"0000000000000000" '- flags, ym2612
print"999E3640" '- ym2151
print"4C000000" '- offset
print"00000000000000000000000000000000"
print"00000000000000000000000000000000"
print"00000000000000000000000000000000"
print"00000000000000000000000000000000"
print"0000000000000000"
print""
print";- registers reset?"
print"540102 541800 541B00 5419FF 54197F "
print"540800 5420C0 54607F 54687F 54707F 54787F "
print"540801 5421C0 54617F 54697F 54717F 54797F "
print"540802 5422C0 54627F 546A7F 54727F 547A7F "
print"540803 5423C0 54637F 546B7F 54737F 547B7F "
print"540804 5424C0 54647F 546C7F 54747F 547C7F "
print"540805 5425C0 54657F 546D7F 54757F 547D7F "
print"540806 5426C0 54667F 546E7F 54767F 547E7F "
print"540807 5427C0 54677F 546F7F 54777F 547F7F"
print"A40102 A41800 A41B00 A419FF A4197F "
print"A40800 A420C0 A4607F A4687F A4707F A4787F "
print"A40801 A421C0 A4617F A4697F A4717F A4797F "
print"A40802 A422C0 A4627F A46A7F A4727F A47A7F "
print"A40803 A423C0 A4637F A46B7F A4737F A47B7F "
print"A40804 A424C0 A4647F A46C7F A4747F A47C7F "
print"A40805 A425C0 A4657F A46D7F A4757F A47D7F "
print"A40806 A426C0 A4667F A46E7F A4767F A47E7F "
print"A40807 A427C0 A4677F A46F7F A4777F A47F7F"

print""
'print";-?"
'print"C002007F C003007F C00A007F C00B007F"
'print"C012007F C013007F C01A007F C01B007F"
'print"C022007F C023007F"
'print""
print";- instruments?"
print"544032 54601D 54800E 54A00E 54C000 54E04A"
print"544851 546816 54880E 54A80E 54C800 54E83A"
print"545051 547019 549014 54B000 54D000 54F00A"
print"545831 54781E 549814 54B802 54D801 54F83A"
print"5420FB 543800 540878 542800 543000"
print"544132 54611D 54810E 54A10E 54C100 54E14A"
print"544951 546916 54890E 54A90E 54C900 54E93A"
print"545151 547119 549114 54B100 54D100 54F10A"
print"545931 54791E 549914 54B902 54D901 54F93A"
print"5421FB 543900 540879 54290E 543100"
print"544232 54621D 54820E 54A20E 54C200 54E24A"
print"544A51 546A16 548A0E 54AA0E 54CA00 54EA3A"
print"545251 547219 549214 54B200 54D200 54F20A"
print"545A31 547A1E 549A14 54BA02 54DA01 54FA3A"
print"5422FB 543A00 54087A 542A1E 543200"
print"544332 54631D 54830E 54A30E 54C300 54E34A"
print"544B51 546B16 548B0E 54AB0E 54CB00 54EB3A"
print"545351 547319 549314 54B300 54D300 54F30A"
print"545B31 547B1E 549B14 54BB02 54DB01 54FB3A"
print"5423FB 543B00 54087B 542B2E 543300"
print"544432 54641D 54840E 54A40E 54C400 54E44A"
print"544C51 546C16 548C0E 54AC0E 54CC00 54EC3A"
print"545451 547419 549414 54B400 54D400 54F40A"
print"545C31 547C1E 549C14 54BC02 54DC01 54FC3A"
print"5424FB 543C00 54087C 542C3E 543400"
print"544532 54651D 54850E 54A50E 54C500 54E54A"
print"544D51 546D16 548D0E 54AD0E 54CD00 54ED3A"
print"545551 547519 549514 54B500 54D500 54F50A"
print"545D31 547D1E 549D14 54BD02 54DD01 54FD3A"
print"5425FB 543D00 54087D 542D4E 543500"
print"544632 54661D 54860E 54A60E 54C600 54E64A"
print"544E51 546E16 548E0E 54AE0E 54CE00 54EE3A"
print"545651 547619 549614 54B600 54D600 54F60A"
print"545E31 547E1E 549E14 54BE02 54DE01 54FE3A"
print"5426FB 543E00 54087E 542E5E 543600"
print"544732 54671D 54870E 54A70E 54C700 54E74A "
print"544F51 546F16 548F0E 54AF0E 54CF00 54EF3A "
print"545751 547719 549714 54B700 54D700 54F70A "
print"545F31 547F1E 549F14 54BF02 54DF01 54FF3A "
print"5427FB 543F00 54087F 542F6E 543700"
print"A44032 A4601D A4800E A4A00E A4C000 A4E04A"
print"A44851 A46816 A4880E A4A80E A4C800 A4E83A"
print"A45051 A47019 A49014 A4B000 A4D000 A4F00A"
print"A45831 A4781E A49814 A4B802 A4D801 A4F83A"
print"A420FB A43800 A40878 A42800 A43000"
print"A44132 A4611D A4810E A4A10E A4C100 A4E14A"
print"A44951 A46916 A4890E A4A90E A4C900 A4E93A"
print"A45151 A47119 A49114 A4B100 A4D100 A4F10A"
print"A45931 A4791E A49914 A4B902 A4D901 A4F93A"
print"A421FB A43900 A40879 A4290E A43100"
print"A44232 A4621D A4820E A4A20E A4C200 A4E24A"
print"A44A51 A46A16 A48A0E A4AA0E A4CA00 A4EA3A"
print"A45251 A47219 A49214 A4B200 A4D200 A4F20A"
print"A45A31 A47A1E A49A14 A4BA02 A4DA01 A4FA3A"
print"A422FB A43A00 A4087A A42A1E A43200"
print"A44332 A4631D A4830E A4A30E A4C300 A4E34A"
print"A44B51 A46B16 A48B0E A4AB0E A4CB00 A4EB3A"
print"A45351 A47319 A49314 A4B300 A4D300 A4F30A"
print"A45B31 A47B1E A49B14 A4BB02 A4DB01 A4FB3A"
print"A423FB A43B00 A4087B A42B2E A43300"
print"A44432 A4641D A4840E A4A40E A4C400 A4E44A"
print"A44C51 A46C16 A48C0E A4AC0E A4CC00 A4EC3A"
print"A45451 A47419 A49414 A4B400 A4D400 A4F40A"
print"A45C31 A47C1E A49C14 A4BC02 A4DC01 A4FC3A"
print"A424FB A43C00 A4087C A42C3E A43400"
print"A44532 A4651D A4850E A4A50E A4C500 A4E54A"
print"A44D51 A46D16 A48D0E A4AD0E A4CD00 A4ED3A"
print"A45551 A47519 A49514 A4B500 A4D500 A4F50A"
print"A45D31 A47D1E A49D14 A4BD02 A4DD01 A4FD3A"
print"A425FB A43D00 A4087D A42D4E A43500"
print"A44632 A4661D A4860E A4A60E A4C600 A4E64A"
print"A44E51 A46E16 A48E0E A4AE0E A4CE00 A4EE3A"
print"A45651 A47619 A49614 A4B600 A4D600 A4F60A"
print"A45E31 A47E1E A49E14 A4BE02 A4DE01 A4FE3A"
print"A426FB A43E00 A4087E A42E5E A43600"
print"A44732 A4671D A4870E A4A70E A4C700 A4E74A "
print"A44F51 A46F16 A48F0E A4AF0E A4CF00 A4EF3A "
print"A45751 A47719 A49714 A4B700 A4D700 A4F70A "
print"A45F31 A47F1E A49F14 A4BF02 A4DF01 A4FF3A "
print"A427FB A43F00 A4087F A42F6E A43700"

lct=0

open finp$ for input as #1
while eof(1)=0
  file input #1,txrd$
  setcaption("b:"+str$(lct)):lct+=1
  if mid$(txrd$,1,1)="[" then:
    if mid$(txrd$,2,1)="f" then:
      chid=val(mid$(txrd$,3,2))
      chid=chid mod 16 'ym2151x2 has 16 channels
      chfqv=vldc(lstinf$(txrd$,":","]"))
      chfqv-=(2/12) '- <- c
      chfqvo=int(chfqv)
      'if chfqvo<1 then:chfqvo=1:end if
      'if chfqvo>8 then:chfqvo=8:end if
      chfqvf=chfqv-chfqvo
      chfqv1a1=chfqvf*12
      chfqv1a2=int(chfqv1a1)
      'chfqv1a2b=chfqv1a2+int((chfqv1a2+2)/3) '- <-
      chfqv1a2b=wlt[chfqv1a2] '- <-
      chfqv1a3=chfqv1a1-chfqv1a2
      chfqv1a3b=(int(chfqv1a3*64))*4

      'chfqv3=int(644*2^chfqvf)
      'chfqv3a=chfqv3 mod 256
      'chfqv3b=int(chfqv3/256)
      'chfqv5=(chfqvo*8+chfqv3b)
      'chfqv2=int(26.6984611843*2^(8-chfqv))
      'if chfqv2>2047 then:chfqv2=2047:end if
      'if chfqv2<0 then:chfqv2=0:end if
      'chchrfq$=right$("000"+hex$(chfqv2),3)

      if int(chid/8)=0 then
        txrgo$="54080"+right$("0"+hex$(chid mod 8),1)
        txrgo$+=" 54087"+right$("0"+hex$(8+(chid mod 8)),1)
        txrgo$+=" 542"+right$("0"+hex$(8+(chid mod 8)),1)+right$("0"+hex$(chfqvo),1)+right$("0"+hex$(chfqv1a2b),1)
        txrgo$+=" 543"+right$("0"+hex$(chid mod 8),1)+right$("00"+hex$(chfqv1a3b),2)
      else
        txrgo$="A4080"+right$("0"+hex$(chid mod 8),1)
        txrgo$+=" A4087"+right$("0"+hex$(8+(chid mod 8)),1)
        txrgo$+=" A42"+right$("0"+hex$(8+(chid mod 8)),1)+right$("0"+hex$(chfqvo),1)+right$("0"+hex$(chfqv1a2b),1)
        txrgo$+=" A43"+right$("0"+hex$(chid mod 8),1)+right$("00"+hex$(chfqv1a3b),2)
        end if

      'txrgo$+=" 54A4"+right$("00"+hex$(chfqv5),2)
      'txrgo$+=" 54A0"+right$("00"+hex$(chfqv3a),2)
      'txrgo$+=" 5428F"+right$("0"+hex$(chid),1)
      print ucase$(txrgo$)
      end if
    'if mid$(txrd$,2,1)="v" then:
    '  chid=val(mid$(txrd$,3,2))
    '  chid=chid mod 16 'ym2151x2 has 16 channels
    '  chvlv=vldc(lstinf$(txrd$,":","]"))*0x10
    '  chchrvl$=right$("00"+hex$(chvlv),2)
    '  txrgo$="A0"+right$("00"+hex$(8+chid),2)+chchrvl$
    '  print ucase$(txrgo$)
    '  end if
    if mid$(txrd$,2,3)="wvb" then:
      wvblv=vldc(lstinf$(txrd$,":","]"))
      wvblv2=wvblv*44100
      wvblv3a=wvblv2 mod 65536
      wvblv3b=int(wvblv2/65536)
      wvblv3as$=ucase$(right$("0000"+hex$(wvblv3a),4))
      for ii=1 to wvblv3b:print"61FFFF":next
      print "61"+mid$(wvblv3as$,3,2)+mid$(wvblv3as$,1,2)
      end if
    end if
  wend
print:print "66":print
close #1

'
' 0 1 2 3 4 5 6 7 8 9 10 11
' -2 0 1 2 4 5 6 8 9 10 12 13
' 0 2 3 4 6 7 8 10 11 12 14 15
' 0 1 1 1 2 2 2 3 3 3 4 4
' n+int((n+2)/3)
'


